package com.rj.bd.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.rj.bd.entity.Book;

@Mapper
public interface BookMapper {

	@Results({
		@Result(column="cname",property="cname"),
		@Result(column="cid",property="class1",one=@One(select="com.rj.bd.dao.ClassMapper.queryById"))
	})
	@Select("select * from book")
	List<Book> queryAll();

	@Select("SELECT book.bid id ,book.bname bname,book.bwriter bwriter,book.bconcern bconcern,book.bnum bnum,book.bprice bprice,book.bimg bimg,book.zhuangtai zhuangtai,class.cid cid,class.cname cname FROM book,class WHERE book.cid=class.cid AND book.bname like #{name} limit #{start},#{end}")
	List<Book> getPage(Map maps, String name);

	@Select("SELECT book.bid id ,book.bname bname,book.bwriter bwriter,book.bconcern bconcern,book.bnum bnum,book.bprice bprice,book.bimg bimg,book.zhuangtai zhuangtai,class.cid cid,class.cname cname FROM book,class WHERE book.cid=class.cid AND book.bname like #{name}")
	List<Book> query(String name);

	@Delete("delete from book where bid=#{bid}")
	void delete(String bid);

	@Insert("insert into book values (#{bid},#{bname},#{bwriter},#{bconcern},#{bnum},#{bprice},#{bimg},#{class1.cid},#{zhuangtai})")
	void add(Book book);

	@Results({
		@Result(column="cname",property="cname"),
		@Result(column="cid",property="class1",one=@One(select="com.rj.bd.dao.ClassMapper.queryById"))
	})
	@Select("SELECT book.bid id ,book.bname bname,book.bwriter bwriter,book.bconcern bconcern,book.bnum bnum,book.bprice bprice,book.bimg bimg,book.zhuangtai zhuangtai,class.cid cid,class.cname cname from book,class where book.cid=class.cid and bid=#{bid}")
	List<Map> queryById(String bid);
	
	@Update("UPDATE book set zhuangtai='2' where bid=#{bid}")
	void editByZhuangTaiWei(String bid);

	@Update("UPDATE book set zhuangtai='1' where bid=#{bid}")
	void editByBookZhuangTaiWei(String bid);

}
